//
//  ToBeChangeTaskListViewController.m
//  MyProject
//
//  Created by Pei on 16/7/18.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "ToBeChangeTaskListViewController.h"
#import "IHRefreshTableView.h"
#import "TaskManagerCell.h"
#import "CreateTaskViewController.h"
#import "NSMutableArray+Additions.h"
#import "UserInfo.h"
#import "SelectModel.h"
#import "ToBeSureTaskViewController.h"
#import "ToBeChangeTaskDetailViewController.h"
#import "RequestPostUploadHelper.h"

@interface ToBeChangeTaskListViewController ()<UITableViewDataSource,UITableViewDelegate,IHTableViewRefreshingDelegate, ToBeSureTaskViewControllerDelegate, ToBeChangeTaskDetailViewControllerDelegate>
{
    MySceneType _type;
    
    IHRefreshTableView *_myTable;
    
    UIView *_addView;
    
    UIControl *_addControl;
    
    UIButton *_rightBtn;
    
    NSArray *_contentArray;
    
    NSDictionary *_currentDic;
    
    NSString *_currentStr;
    
    NSMutableArray *saveArray;
    
    NSString *TMP_UPLOAD_IMG_PATH;
    
    UIView *emptyView;
}

@end

@implementation ToBeChangeTaskListViewController

- (id)initWith:(MySceneType)type
{
    self = [super init];
    if (self)
    {
        _type = type;
        
        saveArray = [[NSMutableArray alloc] initWithCapacity:3];
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:Hide_TabBar_Notification object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:Hide_TabBar_Notification object:nil];
    
    //_contentArray = (NSArray*)[[NSUserDefaults standardUserDefaults] objectForKey:ToBeChangeTask_Save];
    //[_myTable reloadData];
    
    if (_contentArray != nil && _contentArray.count > 0) {
        NSMutableArray *tempYYY = [[NSMutableArray alloc] initWithCapacity:3];
        [tempYYY removeAllObjects];
        
        [saveArray removeAllObjects];
        NSArray *arrayTTT = [[NSUserDefaults standardUserDefaults] objectForKey:ToBeChangeTask_Save];
        saveArray = [arrayTTT mutableCopy];
        
        for (int i = 0;i < _contentArray.count; i ++) {
            NSDictionary *ccc = [_contentArray objectAtIndex:i];
            NSMutableDictionary *tempDDDD = [[NSMutableDictionary alloc] initWithDictionary:ccc];
            
            NSMutableArray *arrayAAA = [[NSMutableArray alloc] initWithCapacity:3];
            [arrayAAA removeAllObjects];
            
            for (int j = 0;j < saveArray.count; j++) {
                //NSMutableDictionary *ddddd = [[NSMutableDictionary alloc] initWithDictionary:bbbb];
                NSDictionary *bbbb = [saveArray objectAtIndex:j];
                //[dic setValue:[_dataDic valueForKey:@"InspectItemID"] forKey:@"InspectItemID"];
                //[dic setValue:[_dataDic valueForKey:@"InspectionID"] forKey:@"InspectionID"];
                if (([[ccc valueForKey:@"InspectItemID"] intValue] == [[bbbb valueForKey:@"InspectItemID"] intValue]) && ([[ccc valueForKey:@"InspectionID"] intValue] == [[bbbb valueForKey:@"InspectionID"] intValue])) {
                    [arrayAAA addObject:bbbb];
                }
            }
            
            [tempDDDD setObject:arrayAAA forKey:@"AttachList"];
            [tempYYY addObject:tempDDDD];
            
        }
        
        _contentArray = tempYYY;
    }
    
    if (_myTable != nil) {
        [_myTable reloadData];
    }
}

- (void)getData
{
    [[YMNetLib getInstance] connectWithAction:Get_ToBeChange_Task_List method:@"GET" withContent:@"" withBody:nil withDelegate:self];
    _currentStr = Get_ToBeChange_Task_List;
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([_currentStr isEqualToString:@"Check_Type_Items"]) {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            [[NSUserDefaults standardUserDefaults] setObject:[body valueForKey:@"DicList"] forKey:Check_Type_Items];
            [_myTable reloadData];
        }
    }else if ([_currentStr isEqualToString:Get_ToBeChange_Task_List]){
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
        {
            _currentDic = body;
            
            _contentArray = [_currentDic valueForKey:@"RepairRecordList"];
            
            //[[NSUserDefaults standardUserDefaults] removeObjectForKey:ToBeChangeTask_Save];
            //[[NSUserDefaults standardUserDefaults] synchronize];
            
            /*
             NSMutableArray *tempYYY = [[NSMutableArray alloc] initWithCapacity:3];
             [tempYYY removeAllObjects];
             
             [saveArray removeAllObjects];
             NSArray *arrayTTT = [[NSUserDefaults standardUserDefaults] objectForKey:ToBeChangeTask_Save];
             saveArray = [arrayTTT mutableCopy];
             
             for (int i = 0;i < _contentArray.count; i ++) {
             NSDictionary *ccc = [_contentArray objectAtIndex:i];
             NSMutableDictionary *tempDDDD = [[NSMutableDictionary alloc] initWithDictionary:ccc];
             
             NSMutableArray *arrayTTTT = [[NSMutableArray alloc] initWithCapacity:3];
             [arrayTTTT removeAllObjects];
             
             for (int j = 0;j < saveArray.count; j++) {
             //NSMutableDictionary *ddddd = [[NSMutableDictionary alloc] initWithDictionary:bbbb];
             NSDictionary *bbbb = [saveArray objectAtIndex:j];
             //[dic setValue:[_dataDic valueForKey:@"InspectItemID"] forKey:@"InspectItemID"];
             //[dic setValue:[_dataDic valueForKey:@"InspectionID"] forKey:@"InspectionID"];
             if (([[ccc valueForKey:@"InspectItemID"] intValue] == [[bbbb valueForKey:@"InspectItemID"] intValue]) && ([[ccc valueForKey:@"InspectionID"] intValue] == [[bbbb valueForKey:@"InspectionID"] intValue])) {
             [arrayTTTT addObject:bbbb];
             }
             }
             
             [tempDDDD setObject:arrayTTTT forKey:@"AttachList"];
             [tempYYY addObject:tempDDDD];
             
             }
             */
            
            if (_contentArray != nil && _contentArray.count > 0) {
                NSMutableArray *tempYYY = [[NSMutableArray alloc] initWithCapacity:3];
                [tempYYY removeAllObjects];
                
                [saveArray removeAllObjects];
                NSArray *arrayTTT = [[NSUserDefaults standardUserDefaults] objectForKey:ToBeChangeTask_Save];
                saveArray = [arrayTTT mutableCopy];
                
                for (int i = 0;i < _contentArray.count; i ++) {
                    NSDictionary *ccc = [_contentArray objectAtIndex:i];
                    NSMutableDictionary *tempDDDD = [[NSMutableDictionary alloc] initWithDictionary:ccc];
                    
                    NSMutableArray *arrayAAA = [[NSMutableArray alloc] initWithCapacity:3];
                    [arrayAAA removeAllObjects];
                    
                    for (int j = 0;j < saveArray.count; j++) {
                        //NSMutableDictionary *ddddd = [[NSMutableDictionary alloc] initWithDictionary:bbbb];
                        NSDictionary *bbbb = [saveArray objectAtIndex:j];
                        //[dic setValue:[_dataDic valueForKey:@"InspectItemID"] forKey:@"InspectItemID"];
                        //[dic setValue:[_dataDic valueForKey:@"InspectionID"] forKey:@"InspectionID"];
                        if (([[ccc valueForKey:@"InspectItemID"] intValue] == [[bbbb valueForKey:@"InspectItemID"] intValue]) && ([[ccc valueForKey:@"InspectionID"] intValue] == [[bbbb valueForKey:@"InspectionID"] intValue])) {
                            [arrayAAA addObject:bbbb];
                        }
                    }
                    
                    [tempDDDD setObject:arrayAAA forKey:@"AttachList"];
                    [tempYYY addObject:tempDDDD];
                    
                }
                
                _contentArray = tempYYY;
            }
            
            if (_contentArray != nil && _contentArray.count > 0) {
                [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
                
                [emptyView removeFromSuperview];
            }else{
                [self.view addSubview:emptyView];
            }
            
            [_myTable reloadData];
            NSLog(@"==========error%@", [body valueForKey:@"Error"]);
            
            
            [self getCheckData];
        }
    }else{
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            [self getData];
        }
        NSLog(@"==========error%@", [body valueForKey:@"Error"]);
    }
    
}

- (void)getCheckData{
    [[YMNetLib getInstance] connectWithAction:Dictionary_Type_API method:@"GET" withContent:@"DicType=InspectType" withBody:nil withDelegate:self];
    _currentStr = @"Check_Type_Items";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TMP_UPLOAD_IMG_PATH = @"";
    
    emptyView = [[UIView alloc] init];
    emptyView.frame = CGRectMake((kDeviceWidth-45.0f-20.0f)/2.0f, (kDeviceHeight-NAV_HEIGHT-45.0f-20.0f)/2.0f, 45.0f+20.0f, 45.0f+20.0f);
    
    UIImageView *emptyImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"empty"]];
    emptyImageView.frame = CGRectMake(10.0f, 0.0f, 45.0f, 45.0f);
    [emptyView addSubview:emptyImageView];
    
    UILabel *empryLbl = [[UILabel alloc] init];
    empryLbl.frame = CGRectMake(0.0f, 45.0f+8.0f, 45.0f+20.0f, Home_Small_Size);
    empryLbl.font = kAppFont(Home_Small_Size);
    empryLbl.text = @"暂无数据";
    empryLbl.textAlignment = NSTextAlignmentCenter;
    [emptyView addSubview:empryLbl];
    
    
    _rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    //[rightBtn setBackgroundImage:[UIImage imageNamed:@"02edit"] forState:UIControlStateNormal];
    //[rightBtn setBackgroundImage:[UIImage imageNamed:@"02edit"] forState:UIControlStateHighlighted];
    [_rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [_rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    _rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [_rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    //[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
    
    
    
    [self initTableView];
    
    _addControl = [[UIControl alloc] init];
    _addControl.frame = CGRectMake(0.0f, kDeviceHeight-NAV_HEIGHT-kTabHeight, kDeviceWidth, kTabHeight);
    [_addControl addTarget:self action:@selector(addControlAction) forControlEvents:UIControlEventTouchUpInside];
    _addControl.backgroundColor = kBtnBackgroundColor;
    //[self.view addSubview:_addControl];
    
    //02edit@3x
    UILabel *btnTitle = [[UILabel alloc] initWithFrame:CGRectMake((kDeviceWidth-66)/2.0f, (_addControl.height-Home_Big_Size)/2.0f, 66, Home_Big_Size)];
    btnTitle.textColor = kWhiteColor;
    btnTitle.text = @"添加任务";
    btnTitle.font = kAppFont(Home_Big_Size);
    [_addControl addSubview:btnTitle];
    
    _addView = [[UIView alloc] init];
    _addView.hidden = YES;
    _addView.frame = CGRectMake(0.0f, kDeviceHeight-NAV_HEIGHT-kTabHeight, kDeviceWidth, kTabHeight);
    [self.view addSubview:_addView];
    
    //114 122 162
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth/2.0f, kTabHeight);
    [cancleBtn setBackgroundColor:kRGB(114, 122, 162)];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = kAppFont(Home_Big_Size);
    cancleBtn.tag = 116;
    [cancleBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_addView addSubview:cancleBtn];
    
    UIButton *uploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    uploadBtn.frame = CGRectMake(kDeviceWidth/2.0f, 0.0f, kDeviceWidth/2.0f, kTabHeight);
    [uploadBtn setBackgroundColor:kBtnBackgroundColor];
    [uploadBtn setTitle:@"提交" forState:UIControlStateNormal];
    [uploadBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    uploadBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [uploadBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    uploadBtn.tag = 118;
    [_addView addSubview:uploadBtn];
    
    /*
     UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
     deleteBtn.frame = CGRectMake(kDeviceWidth/3.0f*2.0f, 0.0f, kDeviceWidth/3.0f, kTabHeight);
     [deleteBtn setBackgroundColor:kRedColor];
     [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
     [deleteBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
     deleteBtn.titleLabel.font = kAppFont(Home_Big_Size);
     deleteBtn.tag = 119;
     [deleteBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
     [_addView addSubview:deleteBtn];
     */
    //    NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:Check_Type_Items];
    //    if (tempArray != nil && tempArray.count > 0) {
    //
    //    }else{
    //        [self getCheckData];
    //    }
    
    
    [self getData];
}

#pragma mark **************************** UI *********************************
- (void)initTableView
{
    CGRect frame = CGRectMake(0, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
    _myTable = [[IHRefreshTableView alloc] initWithFrame:frame refreshType:-1 andAutoLoad:NO];
    _myTable.delegate = self;
    _myTable.dataSource = self;
    _myTable.refreshDelegate = self;
    _myTable.footer.hidden = YES;
    _myTable.header.hidden = YES;
    _myTable.allowsSelectionDuringEditing = YES;
    [self.view addSubview:_myTable];
}

#pragma mark ************************** Action ************************
- (void)rightBtnAcion
{
    [_myTable setEditing:!_myTable.editing animated:YES];
    
    _addControl.hidden = YES;
    _addView.hidden = NO;
    
    [self.navigationItem setRightBarButtonItem:nil];
    
    _myTable.frame = CGRectMake(0, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT-kTabHeight);
}

- (void)addControlAction
{
    CreateTaskViewController *createTaskVC = [[CreateTaskViewController alloc] init];
    [self.navigationController pushViewController:createTaskVC animated:YES];
}


- (void)btnAction:(UIButton*)sender
{
    switch (sender.tag) {
        case 116:
        {
            [_myTable setEditing:!_myTable.editing animated:YES];
            
            _addView.hidden = YES;
            _addControl.hidden = NO;
            
            [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
            
            _myTable.frame = CGRectMake(0, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
        }
            break;
        case 119:
        {
            NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:_contentArray];
            for (int i = 0; i < _contentArray.count; i++)
            {
                NSDictionary *tempD = [_contentArray objectAtIndex:i];
                if ([[tempD valueForKey:@"isSelect"] boolValue]) {
                    //[tempArray replaceObjectAtIndex:i withObject:@""];
                    [tempArray removeObject:tempD];
                }
            }
            _contentArray = tempArray;
            
            [[NSUserDefaults standardUserDefaults] setObject:_contentArray forKey:Create_Task];
            _contentArray = (NSArray*)[[NSUserDefaults standardUserDefaults] objectForKey:Create_Task];
            
            [_myTable reloadData];
            
            [_myTable setEditing:!_myTable.editing animated:YES];
            
            _addView.hidden = YES;
            _addControl.hidden = NO;
            
            [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
            
            _myTable.frame = CGRectMake(0, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
            
        }
            break;
        case 118:
        {
            [[YMUIUtil getInstance] hideLoadingView];
            [[YMUIUtil getInstance] showLoadingWithTitle:@"加载中..."];
            
            NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:_contentArray];
            
            if (tempArray == nil || tempArray.count <= 0) {
                [[YMUIUtil getInstance] hideLoadingView];
            }else{
                BOOL isSelect = NO;
                for (int i = 0; i < _contentArray.count; i++)
                {
                    NSDictionary *tempD = [_contentArray objectAtIndex:i];
                    if ([[tempD valueForKey:@"isSelect"] boolValue]){
                        isSelect = YES;
                    }
                }
                
                if (!isSelect) {
                    [[YMUIUtil getInstance] hideLoadingView];
                }
            }
            
            [self performSelector:@selector(uploadTestAction) withObject:nil afterDelay:1.0f];
            
        }
            break;
        default:
            break;
    }
}

- (void)uploadTestAction{
    //NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:_contentArray];
    for (int i = 0; i < _contentArray.count; i++)
    {
        NSDictionary *tempD = [_contentArray objectAtIndex:i];
        if ([[tempD valueForKey:@"isSelect"] boolValue]) {
            
            NSMutableArray *RepairRecordList = [[NSMutableArray alloc] initWithCapacity:3];
            [RepairRecordList removeAllObjects];
            
            NSMutableArray *writeArray = [[NSMutableArray alloc] initWithArray:[tempD objectForKey:@"AttachList"]];
            
            
            if (writeArray == nil || writeArray.count <= 0) {
                [[YMUIUtil getInstance] showTipViewAutoDis:@"请先完善填写整改"];
                [[YMUIUtil getInstance] hideLoadingView];
                
                [_myTable setEditing:!_myTable.editing animated:YES];
                
                _addView.hidden = YES;
                _addControl.hidden = NO;
                
                [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
                
                _myTable.frame = CGRectMake(0, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
                
                return;
            }
            
            for (NSDictionary *yyyy in writeArray) {
                
                NSArray *imageList = [yyyy objectForKey:@"AttachList"];
                
                NSMutableArray *idList = [[NSMutableArray alloc] initWithCapacity:3];
                [idList removeAllObjects];
                
                if (imageList == nil || imageList.count <= 0) {
                    [[YMUIUtil getInstance] showTipViewAutoDis:@"请先完善填写整改"];
                    [[YMUIUtil getInstance] hideLoadingView];
                    
                    [_myTable setEditing:!_myTable.editing animated:YES];
                    
                    _addView.hidden = YES;
                    _addControl.hidden = NO;
                    
                    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
                    
                    
                    _myTable.frame = CGRectMake(0, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
                    
                    return;
                }
                
                for (int j = 0; j < imageList.count; j ++) {
                    
                    //[hImageView setImage:tempImage];
                    NSLog(@"===TMP_UPLOAD_IMG_PATH===%@",TMP_UPLOAD_IMG_PATH);
                    NSData* imageData = [imageList objectAtIndex:j];//UIImagePNGRepresentation(tempImage);
                    
                    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
                    
                    NSString* documentsDirectory = [paths objectAtIndex:0];
                    
                    // Now we get the full path to the file
                    
                    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",[self generateUuidString],@".jpg"]];
                    
                    // and then we write it out
                    TMP_UPLOAD_IMG_PATH=fullPathToFile;
                    NSArray *nameAry=[TMP_UPLOAD_IMG_PATH componentsSeparatedByString:@"/"];
                    NSLog(@"===new fullPathToFile===%@",fullPathToFile);
                    NSLog(@"===new FileName===%@",[nameAry objectAtIndex:[nameAry count]-1]);
                    
                    [imageData writeToFile:fullPathToFile atomically:NO];
                    
                    //[self onPostData];
                    
                    NSMutableDictionary * dir=[NSMutableDictionary dictionaryWithCapacity:7];
                    //[dir setValue:@"save" forKey:@"m"];
                    [dir setValue:[[UserInfo getInstance] getUserID] forKey:@"UserID"];
                    [dir setValue:[[UserInfo getInstance] getUseToken] forKey:@"UserToken"];
                    NSString *url= kAppApi(Upload_File);
                    NSLog(@"=======上传%@", url);
                    if([TMP_UPLOAD_IMG_PATH isEqualToString:@""]){
                        [RequestPostUploadHelper postRequestWithURL:url postParems:dir picFilePath:nil picFileName:nil];
                    }else{
                        NSLog(@"有图标上传");
                        NSArray *nameAry=[TMP_UPLOAD_IMG_PATH componentsSeparatedByString:@"/"];
                        NSString *result =  [RequestPostUploadHelper postRequestWithURL:url postParems:dir picFilePath:TMP_UPLOAD_IMG_PATH picFileName:[nameAry objectAtIndex:[nameAry count]-1]];
                        NSDictionary *resultDic = [self dictionaryWithJsonString:result];
                        
                        
                        NSArray *imageID = [resultDic objectForKey:@"ResourcesList"];
                        for (NSDictionary *ggg in imageID) {
                            [idList addObject:[NSNumber numberWithInt:[[ggg valueForKey:@"ResourceId"] intValue]]];
                        }
                    }
                }
                
                NSMutableDictionary *bbb = [[NSMutableDictionary alloc] initWithCapacity:3];
                [bbb setValue:[NSNumber numberWithInt:[[yyyy valueForKey:@"InspectTaskID"] intValue]] forKey:@"InspectTaskID"];
                //[bbb setObject:[yyyy objectForKey:@"AttachList"] forKey:@"AttachList"];
                [bbb setObject:idList forKey:@"AttachList"];
                [bbb setValue:[yyyy valueForKey:@"RepairDesc"] forKey:@"RepairDesc"];
                [RepairRecordList addObject:bbb];
            }
            
            //            }
            
            
            
            NSDictionary *bodyD = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [[UserInfo getInstance] getUserID],@"UserID",
                                   [NSNumber numberWithInt:[[tempD valueForKey:@"InspectionID"] intValue]],@"InspectionID",
                                   [NSNumber numberWithInt:[[tempD valueForKey:@"InspectItemID"] intValue]],@"InspectionItemID",
                                   RepairRecordList,@"RepairRecordList",
                                   nil];
            
            [[YMNetLib getInstance] connectWithAction:Upload_DaiZhengGai method:@"POST" withContent:nil withBody:bodyD withDelegate:self];
            _currentStr = Upload_DaiZhengGai;
        }
    }
    
    [_myTable setEditing:!_myTable.editing animated:YES];
    
    _addView.hidden = YES;
    _addControl.hidden = NO;
    
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
    
    _myTable.frame = CGRectMake(0, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
    
    //[self performSelector:@selector(getData) withObject:nil afterDelay:1.0f];
    
    //[self getData];
}


- (void)uploadTask:(NSDictionary*)tempD
{
    NSMutableArray *InspectTaskListArray = [[NSMutableArray alloc] initWithCapacity:3];
    NSArray *tempA = [tempD objectForKey:@"InspectionItems"];
    for (NSDictionary *tempD in tempA) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:3];
        [dic setValue:[tempD valueForKey:@"InspectContentID"] forKey:@"InspectItemCode"];
        [dic setValue:[tempD valueForKey:@"InspectContentName"] forKey:@"InspectTaskName"];
        [dic setValue:@"" forKey:@"DeadLineDate"];
        [InspectTaskListArray addObject:dic];
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:[tempD valueForKey:@"InspectDate"]];
    
    //Upload_Task
    NSDictionary *bodyDic = [NSDictionary dictionaryWithObjectsAndKeys:
                             [[UserInfo getInstance] getUserID],@"UserID",//UserID
                             [tempD valueForKey:@"InspectionNo"],@"InspectionNo", //检查单号
                             [NSNumber numberWithInt:[[[tempD objectForKey:@"InspectType"] valueForKey:@"Value"] intValue]], @"InspectType", //检查类型 int  {Code = "SQCP_Security_InspectionType_Daily"; Name = "\U65e5\U5e38\U68c0\U67e5"; Value = 0;},
                             //{"projectCode":"XM201611","orgCode":"YFZX","orgName":"公司本部","unitCode":"region","unitName":"三航局","projectName":"青岛项目部"}
                             [[tempD objectForKey:@"InspectOrgAndProject"] valueForKey:@"orgCode"], @"InspectOrgCode", //被检查公司编码
                             [[tempD objectForKey:@"InspectOrgAndProject"] valueForKey:@"projectCode"], @"InspectProjectCode", //被检查项目编码
                             date, @"InspectDate", //检查时间 DateTime
                             [NSNumber numberWithInt:[[tempD valueForKey:@"InspectionState"] intValue]], @"InspectionState", //检查单状态 int
                             [tempD valueForKey:@"InspectionRemark"], @"InspectionRemark", //检查单备注
                             InspectTaskListArray,@"InspectTaskList",
                             nil];
    [[YMNetLib getInstance] connectWithAction:Upload_Task method:@"POST" withContent:nil withBody:bodyDic withDelegate:self];
    
}

//- (void)NetDoOverSuccess:(NSDictionary*)body
//{
//
//
//    NSLog(@"========error%@",[body valueForKey:@"Error"]);
//}


#pragma mark  ************************* UITableViewDataSource **************************************
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return (nil == _contentArray) ? 0 : _contentArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TaskManagerCell";
    /*
     TaskManagerCell *cell = (TaskManagerCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
     if (cell == nil)
     {
     cell = [[TaskManagerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
     }
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     [cell showWithData:nil];
     return cell;
     */
    TaskManagerCell *cell = (TaskManagerCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[TaskManagerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundView = [[UIView alloc] init];
    }
    NSDictionary *tempDic = [_contentArray objectAtIndex:indexPath.row];
    cell.mSelected = [(NSNumber *)[tempDic objectForKey:@"isSelect"] boolValue];
    
    [cell showWithDataToBeChange:tempDic];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //[_selFlags moveObjectFromIndex:sourceIndexPath.row toIndex:destinationIndexPath.row];
}


#pragma mark -
#pragma mark Table view delegate

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *tempDic = [_contentArray objectAtIndex:indexPath.row];
    
    if (tableView.isEditing)
    {
        
        TaskManagerCell *cell = (TaskManagerCell *)[tableView cellForRowAtIndexPath:indexPath];
        [cell changeMSelectedState];
        NSDictionary *tempDic = [_contentArray objectAtIndex:indexPath.row];
        NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:_contentArray];
        for (int i = 0; i < tempArray.count; i++)
        {
            NSMutableDictionary *tempD = [[NSMutableDictionary alloc] initWithDictionary:[tempArray objectAtIndex:i]];
            if (([[tempDic valueForKey:@"InspectionID"] intValue] == [[tempD valueForKey:@"InspectionID"] intValue]) && ([[tempDic valueForKey:@"InspectItemID"] intValue] == [[tempD valueForKey:@"InspectItemID"] intValue])) {
                [tempD setObject:[NSNumber numberWithBool:cell.mSelected] forKey:@"isSelect"];
                [tempArray replaceObjectAtIndex:i withObject:tempD];
            }
        }
        _contentArray = tempArray;
        [_myTable reloadData];
    }
    else
    {
        ToBeChangeTaskDetailViewController *changeVC = [[ToBeChangeTaskDetailViewController alloc] initWithDic:tempDic];
        changeVC.delegate = self;
        [self.navigationController pushViewController:changeVC animated:YES];
    }
}

- (void)reloadData{
    [self getData];
}

- (void)refreshData
{
    [self getData];
}

- (NSString *)generateUuidString
{
    // create a new UUID which you own
    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
    
    // create a new CFStringRef (toll-free bridged to NSString)
    // that you own
    NSString *uuidString = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid));
    
    // transfer ownership of the string
    // to the autorelease pool
    //[uuidString autorelease];
    
    // release the UUID
    CFRelease(uuid);
    
    return uuidString;
}

- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}



@end
